
package com.etc.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.expression.ParseException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.etc.entity.Admin;
import com.etc.entity.Music;
import com.etc.entity.Talk;
import com.etc.entity.User;
import com.etc.service.MusicService;
import com.etc.utils.FTPUtil;
import com.etc.utils.IDUtils;
import com.etc.utils.TalkExtent;
import com.github.pagehelper.PageInfo;

@Controller
public class MusicController {
	@Autowired
	private MusicService musicService;

	@Value("${MEDIA_SERVER}")
	private String MEDIA_SERVER;

	/**
	 * 分页展示数据
	 * 
	 * @param pageSize
	 *            分页大小
	 * @param pageNum
	 *            第几页
	 * @return
	 */

	@RequestMapping("/earlist")
	public ModelAndView pageMusic(
			@RequestParam(value = "pageSize", required = true, defaultValue = "4") Integer pageSize,
			@RequestParam(value = "pageNum", required = true, defaultValue = "1") Integer pageNum,
			@RequestParam(value = "mstatus", required = true, defaultValue = "yes") String mstatus) {
		PageInfo<Music> pageInfo = musicService.pageMusic(pageSize, pageNum, mstatus);
		ModelAndView modelAndView = new ModelAndView("ear");
		modelAndView.addObject("list", pageInfo.getList());
		modelAndView.addObject("page", pageInfo);
		return modelAndView;
	}

	
	
	/**
	 * 
	 * 文件下载
	 * 
	 * @param fileName
	 *            文件名
	 * @return
	 * @throws IOException
	 */

	@RequestMapping("/download/{fileName:.+}")
	public ResponseEntity<byte[]> fileDownload(@PathVariable("fileName") String fileName, HttpServletRequest request)
			throws IOException {
		FTPUtil.downFile(fileName, MEDIA_SERVER);
		File file = new File(MEDIA_SERVER + "/" + fileName); // 解决下载的文件没有中文显示
		fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1"); // 构建响应报文的头部
		HttpHeaders headers = new HttpHeaders(); // 内容的类型，二进制流
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); // 文件的名称
		headers.setContentDispositionFormData("attachment", fileName);

		return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
	}

	/**
	 * 获取歌曲详情和对应的评论
	 * 
	 * @param pageSize
	 *            一页的大小
	 * @param pageNum
	 *            第几页
	 * @param mid
	 *            歌曲编号
	 * @param tcategory
	 *            评论类型
	 * @param ttopicid
	 *            父评论ID
	 * @return
	 * @throws ParseException
	 */

	@RequestMapping("/ear_detail")
	public ModelAndView musicDetail(

			@RequestParam(value = "pageSize", required = true, defaultValue = "4") Integer pageSize,

			@RequestParam(value = "pageNum", required = true, defaultValue = "1") Integer pageNum,

			@RequestParam(value = "mid", required = true, defaultValue = "1") Integer mid,

			@RequestParam(value = "tcategory", required = true, defaultValue = "3") String tcategory,

			@RequestParam(value = "ttopicid", required = true, defaultValue = "0") Integer ttopicid)
			throws ParseException {
		ModelAndView modelAndView = new ModelAndView("ear_detail");

		Music music = musicService.selectById(mid);
		PageInfo<Talk> pageInfo = musicService.selecttalkMassages(mid, tcategory, ttopicid, pageSize, pageNum);
		List<Talk> list = pageInfo.getList();
		List<TalkExtent> talkextentlist = new ArrayList<>();
		for (Talk talk : list) {
			TalkExtent talkExtent = new TalkExtent();
			int count = musicService.selectchildCount(talk);
			talkExtent.setTalk(talk);
			talkExtent.setChildCount(count);
			talkextentlist.add(talkExtent);
		}
		for (TalkExtent talkExtent : talkextentlist) {
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy年-MM月-dd日 HH:mm:ss");
			String time = formatter.format(talkExtent.getTalk().getTcreatetime());
			talkExtent.setTime(time);
		}
		int count = musicService.selectCount(mid, tcategory);
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = formatter.format(music.getMcreatetime());

		modelAndView.addObject("music", music);
		modelAndView.addObject("time", time);
		modelAndView.addObject("list", talkextentlist);
		modelAndView.addObject("count", count);
		modelAndView.addObject("page", pageInfo);
		return modelAndView;
	}

	/**
	 * 添加歌曲的评论
	 * 
	 * @param talk
	 *            评论
	 * @param session
	 *            用于取登录用户的信息
	 * @return
	 */

	@RequestMapping("/ear_add")

	@ResponseBody
	public TalkExtent talkadd(Talk talk, HttpSession session) {
		User user = (User) session.getAttribute("user");
		TalkExtent talkExtent = new TalkExtent();
		if (user != null) {
			if (talk.getTtopicid() == null) {
				talk.setTtopicid(0);
			}
			talk.settUid(user.getUid());
			Talk tal = musicService.addMusicTalk(talk);
			talkExtent.setTalk(tal);
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy年-MM月-dd日 HH:mm:ss");
			String time = formatter.format(talkExtent.getTalk().getTcreatetime());
			talkExtent.setTime(time);
			talkExtent.setChildCount(0);

		}
		return talkExtent;
	}

	/**
	 * 获取子评论
	 * 
	 * @param mid
	 *            歌曲编号
	 * @param tcategory
	 *            评论类型
	 * @param ttopicid
	 *            父评论ID
	 * @return
	 */

	@RequestMapping("/ear_child")

	@ResponseBody
	public List<TalkExtent> selectchild(

			@RequestParam(value = "tarticleid", required = true, defaultValue = "1") Integer mid,

			@RequestParam(value = "tcategory", required = true, defaultValue = "3") String tcategory,

			@RequestParam(value = "ttopicid", required = true, defaultValue = "0") Integer ttopicid) {
		List<TalkExtent> massage = musicService.selecttalkMassage(mid, tcategory, ttopicid);
		for (TalkExtent talkExtent : massage) {
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy年-MM月-dd日 HH:mm:ss");
			String time = formatter.format(talkExtent.getTalk().getTcreatetime());
			talkExtent.setTime(time);
		}
		return massage;
	}

}
